package com.dumian.apple.file;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.view.Menu;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.setsuna.fileoperationdemo.R;

import java.io.File;

public class SDCardActivity extends Activity {
	private TextView txtsdCardInfo;
	private ProgressBar proSdcardInfo;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sdcard);
		txtsdCardInfo=(TextView)this.findViewById(R.id.sdCardInfo);
		proSdcardInfo=(ProgressBar)this.findViewById(R.id.progressBar);
		//判断是否有插入存储卡
		if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
			//获取Sd卡所在的目录
			File path=Environment.getExternalStorageDirectory();
			//获得sdcard文件路径
			StatFs statFs=new StatFs(path.getPath());
			//获得block的大小
			long blockSize=statFs.getBlockSize();
			long totalBlocks=statFs.getBlockCount();
			long availableBlocks=statFs.getAvailableBlocks();
			long freeBlocks=statFs.getFreeBlocks();
			//String content=BuildCardInfoString(path,blockSize,totalBlocks,availableBlocks,freeBlocks);
			txtsdCardInfo.setText("path="+path+"\n"+
					"blockSize="+blockSize+"\n"+
					"totalBlocks="+totalBlocks+"\n"+
					"已用空间="+(availableBlocks-freeBlocks)+"\n"+
					"avaliableBlocks="+availableBlocks+"\n"+
					"freeBlocks="+freeBlocks);
			proSdcardInfo.setMax(100);
			proSdcardInfo.setProgress((int)(100-(((double)(availableBlocks))/totalBlocks)*100));
		}else if(Environment.getExternalStorageState().equals(Environment.MEDIA_REMOVED)){
			Toast.makeText(this, "没有SDka", Toast.LENGTH_LONG).show();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.sdcard, menu);
		return true;
	}

}
